<?php

use GuzzleHttp\Client;

class ApiService
{
    public $config = [
        //API接口KEY
        'api_key' => 'ea516db225b7fea7c4aa447bc7ab6530',
        //API接口SECRET,MD5加密时使用
        'api_secret' => '92f1f24f92ed859e76beebdc1d997edf',
        //自己的RSA公钥
        'public_key' => 'MFwwDQYJKoZIhvcNAQEBBQADSwAwSAJBAKFANdq+oKjfUTZSIdczSiPm67q55QoXugWnjmmwqRRWXYInfXNt6ZeGqAttwGjnZDJROR9jCrwhQBfTyqe/uUECAwEAAQ==',
        //自己的RSA私钥
        'private_key' => 'MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEAoUA12r6gqN9RNlIh1zNKI+brurnlChe6BaeOabCpFFZdgid9c23pl4aoC23AaOdkMlE5H2MKvCFAF9PKp7+5QQIDAQABAkEAkVny8Znx8idS6ldAn95gz1oBVVDqD53xldVCvyx6TJRa6WoBfS1SgIYTmZwCLu0Pwm3gShSjZ5BHMtXu2kzE/QIhAM0cx54JwqlCPcN0+sjlnaZoyWuXCE6Ae+0IQRhnvSIbAiEAyUGqwoigy8fa3qtmF85hu5r5JEsWjOA7hy3VRvKnp9MCID9K1TIoMuBF54crzsM1T4ttJFJF3bNTpGoFNQkR4ePnAiEAw7RQieT0f9TzV85Sc9nkHdugZBOEdS9jk5sVJ221bLMCIBLVfSrVLdY34QKO4LsJZlntR4SI81DPN3YUMjnHyKWq',
        //平台的RSA公钥
        'platform_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh+zjB/OugBcj4z81Tz0cozN0v1llUiwLt4luoaSiMUGj4qlA2FZQlgISykZZy/d7zoplha61UtA5WEuaH0DD146E9T7/ktVGTV3PhtJUtvnsfhaPCkBMdE+WCDmq2sr8W32VLRnZhuYNOOOkj7eDEXucFJZiSgX/h/QpaHuB+lDMWpV6koMQm6EfkndEn7iuNGyzOgPfePTjSpb9v+SJcsuO2+SOLKlauhSM6qb+3gFk9KlkQ4mhKsgDxjU7cm4PWK8LkrgPbLlgoOVdTdI8yyfYhbzzAeHWnLYjsAvjPLQcy32qHazClxpAHokZ+icVveJFpKt2t2E7b7n3tBtVVwIDAQAB',
        //接口路径
        'base_url' => 'https://b.thongyule.cn/',
        'notify_url' => 'http://XX/notify.php',
        'notify_draw_url' => 'http://XXX/notify_draw.php',
        'return_url' => 'http://XXX/return.php',
        'sign_type' => 'RSA2'
    ];

    //请求
    public function CurlUrl($url, $params = [], $method = "GET")
    {
        // require_once("./extend/618pay/vendor/guzzlehttp/guzzle/src/Client.php");
        try {
            $client = new Client([
                'timeout' => 30
            ]);

            $post_data = [
                'json' => $params,
                'headers' => [
                    'User-Agent' => 'test/1.0',
                    'Content-type' => 'application/json; charset=UTF-8',
                ]
            ];

            $response = $client->request($method, $url, $post_data);

            return json_decode($response->getBody()->getContents(), true);
        } catch (\GuzzleHttp\Exception\ConnectException $e) {
            die('连接失败~');
        }
    }

    //签名
    public function sign618($data, $key, $sign_type)
    {
        require_once("./extend/618pay/Service/RSAService.php");
        $getdata = $data;
        if (isset($getdata['sign'])) {
            unset($getdata['sign']);
        }

        if ($sign_type == 'MD5') {
            $datastr = $this->joinMapValue($data);
            $sign = md5(mb_strtoupper($datastr . '&secret=' . $key));
        } else {
            $rsa = new \RSAService();
            $datastr = $this->joinMapValue($getdata);
            $datastr = mb_strtoupper($datastr);
            $sign = $rsa->sign($key, $datastr, $sign_type);
        }

        return $sign;
    }

    //验证签名
    public function verfiysign618($data, $key, $sign, $sign_type)
    {
        require_once("./extend/618pay/Service/RSAService.php");
        $getdata = $data;
        if (isset($getdata['sign'])) {
            unset($getdata['sign']);
        }

        if ($sign_type == 'MD5') {
            $verfiy = $this->verfiyMd5($getdata, $key, $sign);
        } else {
            $rsa = new \RSAService();
            $datastr = $this->joinMapValue($getdata);
            $datastr = mb_strtoupper($datastr);
            $verfiy = $rsa->verify($key, $datastr, $sign, $sign_type);
        }

        return $verfiy;
    }

    //MD5验签
    private function verfiyMd5($data, $key, $sign)
    {
        $datastr = $this->joinMapValue($data);
        $signstr = md5(mb_strtoupper($datastr . '&secret=' . $key));
        if ($signstr == $sign) {
            return true;
        }
        return false;
    }

    //序列化
    private function joinMapValue($sign_params)
    {
        ksort($sign_params);
        $sign_str = "";
        foreach ($sign_params as $key => $val) {
            if (!empty($val)) {
                $sign_str .= sprintf("%s=%s&", $key, $val);
            }
        }
        return substr($sign_str, 0, -1);
    }
}